package net.disy.cadenza.repository.workflow;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Source {
	public static String SOURCE_DATASOURCE = "dataSource";
	public static String SOURCE_EDITABLE = "editable";
	public static String SOURCE_RESULT_SOURCE_DATA = "resultSourceData";
	private ResultSourceData resultSourceData;
	private String dataSource;
	private boolean editable;
	
	public Source(Node sourceNode){
		NamedNodeMap namedNodeMap = sourceNode.getAttributes();
		Node attrNode = namedNodeMap.getNamedItem(SOURCE_DATASOURCE);
		dataSource = (attrNode != null) ? attrNode.getNodeValue() : "";
		attrNode = namedNodeMap.getNamedItem(SOURCE_EDITABLE);
		editable = (attrNode != null) ? Boolean.parseBoolean(attrNode.getNodeValue()) : false;
		
		NodeList sourceChildNodes = sourceNode.getChildNodes();
		Node currentNode;
		for(int i = 0 ; i < sourceChildNodes.getLength() ; i ++){
			currentNode  = sourceChildNodes.item(i);
			if(currentNode.getNodeType() == Node.ELEMENT_NODE){
				if(currentNode.getNodeName().equalsIgnoreCase(SOURCE_RESULT_SOURCE_DATA)){
					resultSourceData = new ResultSourceData(currentNode);
					continue;
				}
			}
		}
	}

	public ResultSourceData getResultSourceData() {
		return resultSourceData;
	}

	public String getDataSource() {
		return dataSource;
	}

	public boolean isEditable() {
		return editable;
	}
}
